package
{
	import com.xcopy.Screen;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;

	public class Slot25Seat extends Screen
	{
		public const CLICK:String                = 'CLICK';
		public const MONITOR_NONE:String         = 'Normal'; // 拉霸畫面
		public const MONITOR_RESERVE:String      = 'Reserve'; // 保留機台
		public const MONITOR_FACE_1:String       = 'Face1'; // 娃娃圖示
		public const MONITOR_FACE_2:String       = 'Face2';
		public const MONITOR_FACE_3:String       = 'Face3';
		public const MONITOR_FACE_4:String       = 'Face4';
		public const MONITOR_FACE_5:String       = 'Face5';
		public const MONITOR_FACE_6:String       = 'Face6';
		public const MONITOR_FACE_7:String       = 'Face7';
		public const MONITOR_FACE_8:String       = 'Face8';
		public const MONITOR_FACE_9:String       = 'Face9';
		public const MONITOR_FACE_10:String      = 'Face10';
		public const MONITOR_FACE_11:String      = 'Face11';
		public const MONITOR_FACE_12:String      = 'Face12';
		public static const STATE_NONE:Number    = 0;
		public static const STATE_USE:Number     = 1;
		public static const STATE_RESERVE:Number = 2;
		protected var _monitor:String            = MONITOR_NONE;
		protected var _number:Number             = -1;
		protected var _star:Number               = 0;
		protected var _account:String            = '';
		protected var _state:Number              = 0; // 目前機台狀態，0 為無人使用，1 為使用中，2 為保留
		public var alreadySet:Boolean            = false;
		public var CallbackClick:Function        = null;
		public var CallbackOver:Function         = null;
		public var CallbackOut:Function          = null;

		public function Slot25Seat(r:* = null)
		{
			super(r);
		}

		override public function set clip(c:MovieClip):void
		{
			super.clip = c;
			clip.buttonMode = true;
			clip.mouseChildren = false;
			clip.addEventListener(MouseEvent.CLICK, onClick);
			clip.addEventListener(MouseEvent.MOUSE_OVER, onOver);
			clip.addEventListener(MouseEvent.MOUSE_OUT, onOut);
		}

		public function onClick(e:MouseEvent):void
		{
			if (CallbackClick != null && alreadySet)
			{
				CallbackClick(num);
			}
		}

		public function onOver(e:MouseEvent):void
		{
			if (CallbackOver != null && alreadySet)
			{
				CallbackOver(num);
			}
		}

		public function onOut(e:MouseEvent):void
		{
			if (CallbackOut != null && alreadySet)
			{
				CallbackOut(num);
			}
		}

		public function get state():Number
		{
			return _state;
		}

		// 畫面顯示
		public function set monitor(s:String):void
		{
			// Normal, Reserve, Face1 ~ Face12
			_monitor = s;
			clip.oScreen.gotoAndStop(_monitor);
			switch (_monitor)
			{
				case MONITOR_NONE:
				{
					_state = STATE_NONE;
					break;
				}
				case MONITOR_RESERVE:
				{
					_state = STATE_RESERVE;
					break;
				}
				default:
				{
					_state = STATE_USE;
					break;
				}
			}
		}

		public function get monitor():String
		{
			return _monitor;
		}

		// 機台編號 0-299
		public function set num(n:Number):void
		{
			_number = n;
			clip.oNum1.gotoAndStop('Num' + Math.floor(n / 100));
			clip.oNum2.gotoAndStop('Num' + (Math.floor(n / 10) % 10));
			clip.oNum3.gotoAndStop('Num' + (n % 10));
			clip.oBG.gotoAndStop((_number % 2) == 1 ? 'Odd' : 'Even');
		}

		public function get num():Number
		{
			return _number;
		}

		// 目前本機台的玩家帳號
		public function set account(s:String):void
		{
			_account = s;
			clip.txtAccount.visible = true;
			clip.oStar.visible = false;
			if (s.length > 8)
			{
				s = s.substr(0, 8) + '..';
			}
			clip.txtAccount.text = s;
		}

		public function get account():String
		{
			return _account;
		}

		// 目前本機台儲存星數
		public function set star(n:Number):void
		{
			_star = n;
			clip.txtAccount.visible = false;
			clip.oStar.visible = true;
			var i:Number = 0;
			var mc:MovieClip;
			for (i = 1; i <= 5; i++)
			{
				mc = clip.oStar.getChildByName('oStar' + i) as MovieClip;
				if (i <= n)
				{
					mc.gotoAndStop('Enable');
				}
				else
				{
					mc.gotoAndStop('Disable');
				}
			}
		}

		public function get star():Number
		{
			return _star;
		}

		// 將此機台設為沒人坐
		public function reset(no:Number):void
		{
			monitor = MONITOR_NONE;
			num = no;
			star = 0;
			if (num % 2 == 1)
			{
				clip.oBG.gotoAndStop('Odd');
			}
			else
			{
				clip.oBG.gotoAndStop('Even');
			}
		}

		// 本機台有人坐下了
		public function sit(name:String, face:String, keep:Number):void
		{
			account = name;
			if (keep > 0)
			{
				monitor = MONITOR_RESERVE;
			}
			else
			{
				monitor = 'Face' + face;
			}
		}

		// 本機台玩家離坐
		public function leave(stars:Number):void
		{
			monitor = MONITOR_NONE;
			star = stars;
		}
	}
}
